package com.song.controller;

import com.github.pagehelper.PageInfo;
import com.song.pojo.ResultVO;
import com.song.pojo.Team;
import com.song.pojo.TeamQueryVO;
import com.song.service.TeamService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**球队的控制层
 * @author song
 */
@Controller
@ResponseBody
@RequestMapping("team")
public class TeamController {
    @Resource
    private TeamService teamService;

    //Date类型转换
    @InitBinder
    protected void initDateFormatBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
    }



    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResultVO<Team> queryTeams(Integer pageNum, Integer pageSize, TeamQueryVO vo){
       /* if (pageNum == null || pageNum < 0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 0){
            pageSize = 5;
        }*/
        PageInfo<Team> teamPageInfo = teamService.queryTeams(pageNum, pageSize, vo);
        return new ResultVO<>(teamPageInfo);
    }

    /**
     * 新增
     * @param team
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResultVO insertTeam(Team team){
        Boolean aBoolean = teamService.insertTeam(team);
        if (aBoolean){
            return new ResultVO();
        }else {
            return new ResultVO(500,"添加出现错误，请稍后再进行尝试");
        }
    }

    /**
     * 通过ID查询球队信息
     * @param teamId 球队ID
     * @return 球队对象
     */
    @RequestMapping(value="{id}",method = RequestMethod.GET)
    public ResultVO<Team> queryByTeamId(@PathVariable("id") Integer teamId){
        Team team = teamService.queryByTeamId(teamId);
        if (team != null){
            return new ResultVO<>(team);
        }
        return new ResultVO<>(2001,"您查询的球队不存在");
    }

    /**
     * 更新球队信息
     * @param team 更新球队对象
     * @return
     */
    @RequestMapping(value="{id}",method = RequestMethod.PUT)
    public ResultVO updateTeam(@PathVariable("id") Integer teamId,Team team){
        team.setTeamId(teamId);
        Integer integer = teamService.updateTeam(team);
        if (integer == 1){
            return new ResultVO();
        }else {
            return new ResultVO(500,"添加出现错误，请稍后再进行尝试");
        }
    }

    /**
     * 根据主键删除
     * @param teamId
     * @return
     */
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVO deleteTeam(@PathVariable("id") Integer teamId){
        Team team = teamService.queryByTeamId(teamId);
        team.setIsDel(1);
        Integer integer = teamService.deleteTeam(team);
        if (integer == 1){
            return new ResultVO();
        }else {
            return new ResultVO(500,"删除失败，请稍后再进行尝试");
        }
    }

    /**
     * 文件的上传
     * @return
     */
    @RequestMapping(value = "{id}",method = RequestMethod.POST)
    public ResultVO uploadLogo(@RequestParam("logo")MultipartFile myFile, HttpServletRequest request,@PathVariable("id")Integer teamId){
        //指定文件上传的路径
        String path = request.getServletContext().getRealPath("/img/uploadFile/");
        //获取原始文件后缀
        String originalFilename = myFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String hz = originalFilename.substring(index);
        //随机名称
        String randomName = UUID.randomUUID().toString().replace("-", "");
        String fileName = randomName + hz;
        try {
            myFile.transferTo(new File(path + "/" + fileName));
            System.out.println("上传成功" + path + "/" + fileName);
            //更新数据库中logo信息
            Team team = teamService.queryByTeamId(teamId);
            team.setTeamLogo(fileName);
            Integer mun = teamService.updateTeam(team);
            if (mun == 1){
                return new ResultVO();
            }else {
                return new ResultVO(500,"服务器崩溃，请稍后再进行尝试");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO(500,"图片上传出问题");
        }

    }
}
